package cn.newtest.common.dbf;

/**
 * <p>
 * Title: RiseReport
 * </p>
 * <p>
 * Description: RiseReport
 * </p>
 * <p>
 * Copyright: Copyright (c) 2007
 * </p>
 * <p>
 * Company: SZRiseSoft
 * </p>
 * 
 * @author WangChui(baiyibing@gmail.com)
 * @version 2.0 TODO
 * 
 */
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Formater2 implements IFormater {
	private static final Log log = LogFactory.getLog(Formater2.class);

	public Formater2(int specialOP) {
		this.specialOP = specialOP;
	}

	private int specialOP = 2;

	public ArrayList operate(StringBuffer str, int len, String rowSpliter,
			String spliter, JDBField[] jdbFields) {
		// TODO Auto-generated method stub
		log.debug("specialOP = 2");
		ArrayList alist = new ArrayList();
		String[] rows = StringUtils.split(str.toString(), rowSpliter);
		String[] data = null;
		Object[] tmp = null;
		for (int i = 0; i < rows.length; i++) {
			tmp = new Object[len];
			/*
			 * if(rows[i].endsWith(spliter)){ rows[i] = rows[i]+" "; }
			 */
			data = StringUtils.split(rows[i], spliter);

			// else if (specialOP == 1) {// 特殊操作，企调景气专业
			// tmp[0]="";
			// tmp[1]="";
			// tmp[2]="";
			// tmp[3]=data[1];
			// tmp[4]=data[0];
			// tmp[0]=data[1];
			// tmp[1]=data[0];
			// 对于企调景气专的DBF来说，因为所有的字段都是自定义的，所以我们要将SARP导出格式中的缺省的CODE去掉
			// byb comment 20060313
			// for(int j=5; j<len; j++){
			int k = 0;
			for (int j = 0; j < len; j++) {
				k = j + 1; // 这个处理就是忽略到在private StringBuffer
				// outputData方法中自动加入的outputData.append(code).append(spliter);
				log.debug("jdbFields[" + j + "].getType()="
						+ jdbFields[j].getType() + " data[" + k + "]="
						+ data[k]);
				// (c != 'C' && c != 'N' && c != 'L' && c != 'D' && c != 'F'
				// && c !='B')
				if (jdbFields[j].getType() == 'N') {
					if (data[k].trim().equals(""))
						tmp[j] = new Integer("0");
					else
						tmp[j] = new Integer(data[k]);// 跨过名称代码
				} else if (jdbFields[j].getType() == 'F') {
					tmp[j] = new Float(data[k]);// 跨过名称代码
				} else if (jdbFields[j].getType() == 'B') {
					tmp[j] = new Double(data[k]);// 跨过名称代码
				} else if (jdbFields[j].getType() == 'C') {
					int fieldLen = jdbFields[j].getLength();
					if (fieldLen < data[k].length()) {
						tmp[j] = data[k].substring(0, fieldLen);
					} else {
						tmp[j] = data[k];
					}

				} else if (jdbFields[j].getType() == 'D') {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
					try {
						tmp[j] = sdf.parse(data[k]);// 跨过名称代码
					} catch (Exception ex) {
						ex.printStackTrace();
						tmp[j] = new java.util.Date();
					}
					// tmp[j] = new java.util.Date(data[j-3]);//跨过名称代码
				} else if (jdbFields[j].getType() == 'L') {
					tmp[j] = new Boolean(data[k]);// 跨过名称代码
				} else {
					tmp[j] = data[k];// 跨过名称代码
				}

			}
			alist.add(tmp);
		}
		return alist;

	}

}
